package com.tanhua.management.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.UserInfoMapper;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.dubbo.server.api.MessageDetailApi;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.management.vo.MessageDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageDetailService {

    //远程调用api实现   这是消息详情的api
    @Reference(version = "1.0.0")
    private MessageDetailApi messageDetailApi;

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 消息详情
     *
     * @param id 消息的id
     * @return
     */
    public MessageDetailVo queryMessageDetail(String id) {
        //创建对象,找数据拼凑回这个对象返回给前端
        MessageDetailVo messageDetailVo = new MessageDetailVo();

//
//        messageDetailVo.setCommentCount(publishInfo.getCommentCount());
//        messageDetailVo.setLikeCount(publishInfo.getLikeCount());
//        messageDetailVo.setReportCount(publishInfo.getReportCount());
//        messageDetailVo.setForwardingCount(publishInfo.getForwardingCount());


        //通过publish去获取userId
        Publish publish = this.messageDetailApi.queryPublishByPublishId(id);

        if (ObjectUtil.isEmpty(publish)) {
            return messageDetailVo;
        }
        messageDetailVo.setId(id);//id为动态id
        messageDetailVo.setUserId(Convert.toInt(publish.getUserId()));
        messageDetailVo.setTopState(publish.getTopState());
        messageDetailVo.setCreateDate(publish.getCreated());
        messageDetailVo.setState(publish.getState());
        //取出text，medias给messageDetailVo返回对象
        messageDetailVo.setText(publish.getText());
        messageDetailVo.setMedias(Convert.toStrArray(publish.getMedias()));


        //通过userInfo拿到用户的基本信息
        //  将nickname和userLogo给medias给messageDetailVo返回对象
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", publish.getUserId());
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        messageDetailVo.setNickname(userInfo.getNickName());
        messageDetailVo.setUserLogo(userInfo.getLogo());

        if (ObjectUtil.isEmpty(userInfo)) {
            return messageDetailVo;
        }

        Integer likeCount = 0 ;
        Integer commentCount = 0 ;
        Integer reportCount = 0 ;
        Integer forwardingCount = 0 ;
        List<Comment> commentList = messageDetailApi.queryCommentByPublishId(id);
//        System.out.println(commentList.size());
        for (Comment comment : commentList) {
            if(comment.getCommentType()==1){
                likeCount++;
            }
            if(comment.getCommentType()==2){
                commentCount++;
            }
            if(comment.getCommentType()==4){
                forwardingCount++;
            }
            if(comment.getCommentType()==5){
                reportCount++;
            }
        }

        messageDetailVo.setCommentCount(commentCount);
        messageDetailVo.setLikeCount(likeCount);
        messageDetailVo.setForwardingCount(forwardingCount);
        messageDetailVo.setReportCount(reportCount);
        return messageDetailVo;
    }

}
